package com.burgeon.backend.Rest.coupon.xe;
/**
 * 获取会员优惠券
 */
import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.Coupon;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetUserCoupon extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetUserCoupon(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<Coupon> getData(String phone) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(phone), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)){
            throw new Exception();
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Coupon> data = new ArrayList<Coupon>();
        for (int i = 0; i < jsonArray.length(); i++) {
            Coupon coupon = new Coupon();
            coupon.setId(jsonArray.getJSONArray(i).getString(0));
            coupon.setCode(jsonArray.getJSONArray(i).getString(1));
            coupon.setType("购物券");
            coupon.setCheckno(jsonArray.getJSONArray(i).getString(2));
            coupon.setMoney(jsonArray.getJSONArray(i).getString(3));
            coupon.setDatebeg(jsonArray.getJSONArray(i).getString(4));
            coupon.setDateend(jsonArray.getJSONArray(i).getString(5));
            coupon.setVerifyed(jsonArray.getJSONArray(i).getString(6));
            coupon.setValided(jsonArray.getJSONArray(i).getString(7));
            coupon.setOriginal("0");
            data.add(coupon);
        }

        return data;
    }

    private MultiValueMap Data(String phone) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("TICKETNO");//券号
       // columns.add("TICKET_TYPE");//类型
        columns.add("CHECKNO");//检验码
        columns.add("PARVALUE");//面值
        columns.add("DATEBEG");//开始时间
        columns.add("DATEEND");//结束时间
        columns.add("VERIFYED");//核销标记
        columns.add("VALIDED");//有效标记

        params.put("columns", columns);
        params.put("table", "C_V_TDEFTICKET");//表名

        params.put("count", "true");
        HashMap paramss = new HashMap();
        String ac = "'=" + phone + "'";
        paramss.put("column", "'C_VIP_ID;CARDNO'");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetUserCoupon g = new GetUserCoupon("139.129.93.47:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("17732141036"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
